﻿package units
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.display.Shape;
	import flash.utils.*;

	public class Unit extends MovieClip
	{
		public var unit_type = "base_unit";
		public var game_handler:GameHandler,castle:Castle;
		public var being_dragged = false,drag_x=0,drag_y=0;
		public var ellipse_angle = 1/2;
		
		public var attack_radius = 200;
		public var attack_strength = 1;
		public var attack_frequency = 10;
		public var attack_counter = 500;
		
		public var range_line_style = 0x000000;
		
		public var training_exp = 0;
		
		public var range_circle:Shape;
		
		public var mana_regen = .2;
		public var inside_of;
		
		public function Unit(gH:GameHandler=null,c:Castle=null)
		{
			super();
			game_handler=gH;
			castle=c;
			if(game_handler != null){
				game_handler.player_units.push(this);
				game_handler.addChild(this);
				show_range();
				hide_range();
			}
			
			this.addEventListener(MouseEvent.MOUSE_DOWN,select);
			
			
		}
		
		public function run(){
			attack_counter ++;
			if(range_circle != null){
				range_circle.x = this.x;
				range_circle.y = this.y;
			}
			if(being_dragged){
				this.x+= game_handler.mouse_x - drag_x;
				this.y+= game_handler.mouse_y - drag_y;
				drag_x=game_handler.mouse_x;
				drag_y=game_handler.mouse_y;
			}else{
				var on_top_of = castle.base_object_at(this.x,this.y);
				if(on_top_of != null)
					on_top_of.unit_action(this);
			}
			resize_me();
		}
		
		public function resize_me(){
			var resize_to = (this.y - game_handler.STAGE_HEIGHT/2)/game_handler.STAGE_HEIGHT/2*game_handler.resize_effect;
			this.scaleX = 1 + resize_to;
			this.scaleY = 1 + resize_to;
		}
		
		public function gain_experience(){
			this.training_exp += 1;
			if(training_exp == 1 && unit_type == "base_unit")
				this.removeEventListener(MouseEvent.MOUSE_DOWN,select);
		}
		
		public function attack(){
			if(attack_counter < attack_frequency)
				return;
			var valid_targets = get_valid_targets();
			
			if(valid_targets.length != 0){
				valid_targets[0].hit('normal',attack_strength);
				attack_counter = 0;
				var p = new Projectile_Path(game_handler,this,valid_targets[0]);
			}
		}
		
		public function get_valid_targets(){
			var valid_targets:Array = new Array();
			
			for(var i:int = 0; i < game_handler.enemy_units.length; i++){
				if(game_handler.enemy_units[i].virtual_hp > 0){
					var x_dist = this.x - game_handler.enemy_units[i].x;
					var y_dist = this.y - game_handler.enemy_units[i].y;
					if(x_dist*x_dist/attack_radius/attack_radius + y_dist*y_dist/(attack_radius*ellipse_angle*attack_radius*ellipse_angle) < 1)
						valid_targets.push(game_handler.enemy_units[i]);
				}
					
			}
			return valid_targets;
		}
		
		public function select(evt){
			if(inside_of)
				inside_of.unregister(this);
			
			this.addEventListener(MouseEvent.MOUSE_UP,stop_drag);
			drag_x=game_handler.mouse_x;
			drag_y=game_handler.mouse_y;
			being_dragged = true;
			show_range();
		}
		
		public function stop_drag(evt){
			this.removeEventListener(MouseEvent.MOUSE_UP,stop_drag);
			being_dragged = false;
			deselect();
		}
		
		public function go_home(){
			this.x = 388;
			this.y = 300;
		}
		
		public function deselect(){
			hide_range();
		}
		
		public function die(){
			var my_spot = -1;
			for(var i:int = 0; i < game_handler.player_units.length; i++)
				if(game_handler.player_units[i] == this)
					my_spot = i;
			if(my_spot != -1)
				game_handler.player_units.splice(my_spot,1);
			game_handler.removeChild(range_circle);
			game_handler.removeChild(this);
		}
		
		public function show_range(){
			if(range_circle == null){
				range_circle = new Shape();
				
				range_circle.graphics.lineStyle(3,range_line_style,1);
				
				range_circle.graphics.drawEllipse(attack_radius*2*-.5,attack_radius*2*ellipse_angle*-.5,attack_radius*2,attack_radius*2*ellipse_angle);
				//range_circle.graphics.drawCircle(0,0,attack_radius);
				range_circle.x = this.x;
				range_circle.y = this.y;
				game_handler.addChild(range_circle);
			}
			range_circle.alpha = 1;
		}
		public function hide_range(){
			range_circle.alpha = .1;
		}
	}
}